package com.laboratory.servicecustomer.config;

import com.laboratory.servicecustomer.realm.MyRealm;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.io.IOException;
import java.io.InputStream;

/**
 * @Author acok
 * @description: shiro配置类
 * @Date 2022/12/4 20:51
 * @Version 1.0
 */
@Configuration
public class shiroConfig {
    @Autowired
    MyRealm myRealm;

    @Bean(name = "DefaultWebSecurityManager")
    public DefaultWebSecurityManager DefaultWebSecurityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();
        //MD5加密处理
        HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        //散列三次进行加密
        matcher.setHashIterations(3);
        myRealm.setCredentialsMatcher(matcher);

        defaultWebSecurityManager.setRealm(myRealm);

        //Ehcache缓存配置
        defaultWebSecurityManager.setCacheManager(getEhCacheManage());
        return defaultWebSecurityManager;
    }
    /**
     * 让某个实例的某个方法的返回值注入为Bean的实例
     * Spring静态注入
     * @return
     */
    @Bean
    public MethodInvokingFactoryBean getMethodInvokingFactoryBean(){
        MethodInvokingFactoryBean factoryBean = new MethodInvokingFactoryBean();
        factoryBean.setStaticMethod("org.apache.shiro.SecurityUtils.setSecurityManager");
        factoryBean.setArguments(new Object[]{DefaultWebSecurityManager()});
        return factoryBean;
    }

    /*配置缓存对象*/
    public EhCacheManager getEhCacheManage(){
        EhCacheManager ehCacheManager=new EhCacheManager();
        InputStream in=null;
        try {
            in= ResourceUtils.getInputStreamForPath("classpath:ehcache/ehcache-shiro.xml");
        }catch (IOException e){
            e.printStackTrace();
        }
        CacheManager cacheManager=new CacheManager(in);
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

    //拦截器
    @Bean
    public DefaultShiroFilterChainDefinition DefaultShiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition=new DefaultShiroFilterChainDefinition();
        //拦截说明
        //1、登录url不予以拦截
        definition.addPathDefinition("/user/login","anon");
        //2、注册验证账号唯一性不予以拦截
        definition.addPathDefinition("/user/getUserNameList","anon");
        //注册账号
        definition.addPathDefinition("/user/createUser","anon");
        //错误页面
        definition.addPathDefinition("/backgroundSystem/toError","anon");
        //静态资源放行
        definition.addPathDefinition("/icons/**","anon");
        definition.addPathDefinition("/js/**","anon");
        definition.addPathDefinition("/css/**","anon");
        //除anon声明路径，其他路径需要先审核是否已登录，通过rememberMe方式也可以访问url
        definition.addPathDefinition("/**","authc");
        return definition;
    }
}
